package com.xquant.trade.trade.service.impl;

import com.xquant.trade.trade.service.TradeService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("TradeServiceFactory")
public class TradeServiceFactory implements InitializingBean {

    @Autowired
    private List<TradeService> services;
    private Map<String, TradeService> serviceMap;

    public TradeService getTradeService(String tradeType) {
        return serviceMap.get(tradeType);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        serviceMap = new HashMap<>();
        for (int i = 0; i < services.size(); i++) {
            TradeService service = services.get(i);
            for (int j = 0; j < service.getTradeTypes().length; j++) {
                serviceMap.put(service.getTradeTypes()[j], service);
            }
        }
    }
}
